Option (effect)
Combining
all
ap
product
productMany
Constructors
fromIterable
none
some
Conversions
<A>(nullableValue: A) => Option.Option<NonNullable<A>>
nullableをOptionに変換
code:ts
console.log(Option.fromNullable(undefined)) // None
console.log(Option.fromNullable(null)) // None
console.log(Option.fromNullable(1)) // Some(1)
Option → a
Someから値を取り出す
Noneならthrow
Either → Option
EirherがrightならSome
Either → Option
EirherがleftならSome
nullableな普通の関数を、Optionを返すような関数にlifting
(a -> a|null) -> a -> Option<a>
liftThrowable
code:ts
console.log(Option.toArray(Option.some(1)))
console.log(Option.toArray(Option.none()))
// Output: []
toRefinement
Do notation
Do
code:ts
import * as assert from "node:assert"
import { Option, pipe } from "effect"
const result = pipe(
Option.Do,
Option.bind("x", () => Option.some(2)),
Option.bind("y", () => Option.some(3)),
Option.let("sum", ({ x, y }) => x + y),
Option.filter(({ x, y }) => x * y > 5)
)
assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
bind
bindTo
let
Elements
contains
containsWith
exists
Equivalence
getEquivalence
Error handling
Filtering
普通の述語関数を引数に取る
述語がtrueのときにSomeを返す
<A>(predicate: Predicate<NoInfer<A>>): (self: Option<A>) => Option<A>
partitionMap
Generators
gen
Getters
Guards
isNone
isOption
isSome
Lifting
lift2
liftPredicate
Mapping
as
asVoid
Models
None (interface)
Option (type alias)
OptionUnify (interface)
OptionUnifyIgnore (interface)
Some (interface)
Pattern matching
match
Reducing
reduceCompact
Sequencing
2つのOptionをチェーンする
2つのa -> Option<a>を合成した関数を作る
code:ts
const parse = (s: string): Option.Option<number> => (isNaN(Number(s)) ? Option.none() : Option.some(Number(s)))
const double = (n: number): Option.Option<number> => (n > 0 ? Option.some(n * 2) : Option.none())
// string → Option<number>
const parseAndDouble = Option.composeK(parse, double)
flatMapNullable
tap
Sorting
getOrder
Symbols
TypeId
TypeId (type alias)
Type Lambdas
OptionTypeLambda (interface)
Zipping
utils
Option (namespace)
Value (type alias)
void